/*
 * @Author:  朱理哲
 * @since: 2019-09-24 14:23:27
 * @lastTime: 2019-11-23 11:50:01
 * @LastEditors: 朱理哲
 */
import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:im_flutter_sdk_example/request/request.dart';
import 'package:im_flutter_sdk_example/utils/loading.dart';
import 'package:im_flutter_sdk_example/utils/user.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'pull_to_refresh_style.dart';

export './Icon.dart';
export './custom_appbar.dart';

/// global
class G {
  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  /// toolbar routeName
  static final List toobarRouteNameList = [
    '/index',
    '/alliance',
    '/message',
    '/mine',
  ];

  /// 初始化request
  static final Request req = Request();

  static Future<void> showLoading(
          {String status,
          Widget indicator,
          EasyLoadingMaskType maskType,
          bool dismissOnTap}) =>
      EasyLoading.show(
          status: status,
          indicator: indicator,
          maskType: maskType,
          dismissOnTap: dismissOnTap);

  static Future<void> dismissLoading({bool animation = true}) =>
      EasyLoading.dismiss(animation: animation);

  static Future toast(String text) => Fluttertoast.showToast(
      msg: text,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIos: 1,
      backgroundColor: Colors.black54,
      textColor: Colors.white,
      fontSize: 16.0);

  /// 初始化loading
  static final Loading loading = Loading();

  /// 手动延时
  static sleep({int milliseconds = 1000}) async =>
      await Future.delayed(Duration(milliseconds: milliseconds));

  /// 下拉刷新样式
  static final PullToRefreshStyle pullToRefreshStyle = PullToRefreshStyle();

  /// 获取当前的state
  static NavigatorState getCurrentState() => navigatorKey.currentState;

  /// 获取当前的context
  static BuildContext getCurrentContext() => navigatorKey.currentContext;

  /// 获取屏幕上下边距
  /// 用于兼容全面屏，刘海屏
  static EdgeInsets screenPadding() =>
      MediaQuery.of(getCurrentContext()).padding;

  /// 获取屏幕宽度
  static double screenWidth() => MediaQuery.of(getCurrentContext()).size.width;

  static double ratio = MediaQuery.of(getCurrentContext()).size.width / 375;

  /// 获取屏幕高度
  static double screenHeight() =>
      MediaQuery.of(getCurrentContext()).size.height;

  /// 跳转页面使用 G.pushNamed
  static void pushNamed(String routeName, {Object arguments}) {
    // 如果跳转到toolbar页面  不能返回
    if (toobarRouteNameList.indexOf(routeName) > -1) {
      getCurrentState().pushReplacementNamed(
        routeName,
        arguments: arguments,
      );
    } else {
      getCurrentState().pushNamed(routeName, arguments: arguments);
    }
  }

  static void relaunch(String routeName, {Object arguments}) {
    getCurrentState().popAndPushNamed("/index");
    // // 如果跳转到toolbar页面  不能返回
    // if (toobarRouteNameList.indexOf(routeName) > -1) {
    //   getCurrentState().pushReplacementNamed(
    //     routeName,
    //     arguments: arguments,
    //   );
    // } else {
    //   getCurrentState().pushNamed(routeName, arguments: arguments);
    // }
  }

  /// 返回页面
  static void pop() => getCurrentState().pop();

  /// 底部border
  /// ```
  /// @param {Color} color
  /// @param {bool} show  是否显示底部border
  /// ```
  static Border borderBottom({Color color, bool show = true}) {
    return Border(
        bottom: BorderSide(
            color: (color == null || !show)
                ? (show ? rgba(242, 242, 242, 0.5) : Colors.transparent)
                : color,
            width: 1));
  }

  /// 头部border
  /// ```
  /// @param {Color} color
  /// @param {bool} show  是否显示头部border
  /// ```
  static Border borderTop({Color color, bool show = true}) {
    return Border(
        top: BorderSide(
            color: (color == null || !show)
                ? (show ? rgba(242, 242, 242, 0.5) : Colors.transparent)
                : color,
            width: 1));
  }

  static Widget emptyBox() {
    return Container();
  }

  /// 获取时间戳
  /// 不传值 代表获取当前时间戳
  static int getTime([DateTime time]) {
    if (time == null) {
      return (DateTime.now().millisecondsSinceEpoch / 1000).round();
    } else {
      return (time.millisecondsSinceEpoch / 1000).round();
    }
  }

  /// user信息
  static final User user = User();

  static fullImgPath(path) {
    return "$path";
  }

  /// 数组补全图片地址
  static fullImgPathList(pathList) {
    List<String> fullImgPath = [];
    pathList.forEach((e) {
      fullImgPath.add("$e");
    });
    return fullImgPath;
  }

  // 缩略图 
  static String thumbImage112_80(String path) {
    return "$path?x-oss-process=style/app112_80";
  }


  static String thumbImage224_160(String path) {
    return "$path?x-oss-process=style/app224_160";
  }
}
